<?php

namespace App\Cache;

use App\Common\Singleton;
use Hyperf\Context\ApplicationContext;
use Hyperf\Redis\Redis;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class Cache
{
    use Singleton;
    protected mixed $redis;

    /**
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function __construct()
    {
        $this->redis = ApplicationContext::getContainer()->get(Redis::class);
    }

    public function get($key)
    {
        return $this->redis->get($key);
    }

    public function exists($key)
    {
        return $this->redis->exists($key);
    }

    public function set($key, $data, $seconds = 0): bool
    {
        if (empty($seconds)) {
            return $this->redis->set($key, $data);
        }
        return $this->redis->set($key, $data, $seconds);
    }

    public function setNX($key, $data): bool
    {
        return $this->redis->setnx($key, $data);
    }

    public function expire($key, $seconds = 0): bool
    {
        return $this->redis->expire($key, $seconds);
    }

    public function del($key): int
    {
        return $this->redis->del($key);
    }

    public function hGet($key, $field)
    {
        return $this->redis->hget($key, $field);
    }

    public function hSet($key, $field, $data)
    {
        return $this->redis->hset($key, $field, $data);
    }

    public function hDel($key, $field)
    {
        return $this->redis->hdel($key, $field);
    }

    public function increment($key, $value = 1): int
    {
        return $this->redis->incrBy($key, $value);
    }

    public function ttl($key)
    {
        return $this->redis->ttl($key);
    }

}